package com.hfjyz.tutor.tutorcall.bean;

import java.util.ArrayList;
import java.util.List;

import com.hfjyz.base.util.BaseUtil;

public class TutorCallSearchBean extends TutorCallBean {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -5090587359421020014L;

    private String birthdayFrom = null;
    private String birthdayTo = null;
    private String sexCodes = null;
    private String callAimCodes = null;
    private Boolean isOr = false;

    private List<Object> paramList = null;

    public String getBirthdayFrom() {
        return birthdayFrom;
    }

    public void setBirthdayFrom(String birthdayFrom) {
        this.birthdayFrom = birthdayFrom;
    }

    public String getBirthdayTo() {
        return birthdayTo;
    }

    public void setBirthdayTo(String birthdayTo) {
        this.birthdayTo = birthdayTo;
    }

    public String getSexCodes() {
        return sexCodes;
    }

    public void setSexCodes(String sexCodes) {
        this.sexCodes = sexCodes;
    }

    public String getCallAimCodes() {
        return callAimCodes;
    }

    public void setCallAimCodes(String callAimCodes) {
        this.callAimCodes = callAimCodes;
    }

    public Boolean getIsOr() {
        return isOr;
    }

    public void setIsOr(Boolean isOr) {
        this.isOr = isOr;
    }

    public List<Object> getParamList() {
        return paramList;
    }

    public String getWhereClause(String cAlias, String eAlias) {
        String c = null;
        if (isOr) {
            c = " or ";
        } else {
            c = " and ";
        }
        StringBuilder sb = new StringBuilder();
        paramList = new ArrayList<Object>();
        if (target.getId() != null) {
            beforeAppend(sb, c);
            sb.append(eAlias + ".id=?");
            paramList.add(target.getId());
        }
        if (!BaseUtil.isEmpty(target.getIdNo())) {
            beforeAppend(sb, c);
            sb.append(eAlias + ".idNo like ?");
            paramList.add("%" + target.getIdNo() + "%");
        }
        if (!BaseUtil.isEmpty(target.getName())) {
            beforeAppend(sb, c);
            sb.append(eAlias + ".name like ?");
            paramList.add("%" + target.getName() + "%");
        }
        if (target.getSex() >= 0) {
            beforeAppend(sb, c);
            sb.append(eAlias + ".isMale = ?");
            paramList.add(target.getSex() == 1);
        }
        if (!BaseUtil.isEmpty(sexCodes)) {
            beforeAppend(sb, c);
            String[] sexCodeArray = sexCodes.split(",");
            sb.append(" (");
            for (int i = 0; i < sexCodeArray.length; i++) {
                Integer code = Integer.valueOf(sexCodeArray[i]);
                if (i > 0) {
                    sb.append(" or ");
                }
                sb.append(eAlias + ".isMale = ?");
                paramList.add(code == 1);
            }
            sb.append(") ");
        }
        if (callAim != null && callAim >= 0) {
            beforeAppend(sb, c);
            sb.append(cAlias + ".callAim = ?");
            paramList.add(callAim);
        }

        if (!BaseUtil.isEmpty(callAimCodes)) {
            beforeAppend(sb, c);
            String[] codeArray = callAimCodes.split(",");
            sb.append(" (");
            for (int i = 0; i < codeArray.length; i++) {
                Integer code = Integer.valueOf(codeArray[i]);
                if (i > 0) {
                    sb.append(" or ");
                }
                sb.append(cAlias + ".callAim = ?");
                paramList.add(code);
            }
            sb.append(") ");
        }

        if (!BaseUtil.isEmpty(callName)) {
            beforeAppend(sb, c);
            sb.append(cAlias + ".callName like ?");
            paramList.add("%" + callName + "%");
        }
        if (!BaseUtil.isEmpty(callReason)) {
            beforeAppend(sb, c);
            sb.append(cAlias + ".callReason like ?");
            paramList.add("%" + callReason + "%");
        }
        if (!BaseUtil.isEmpty(callRemark)) {
            beforeAppend(sb, c);
            sb.append(cAlias + ".callRemark like ?");
            paramList.add("%" + callRemark + "%");
        }
        
        if (sb.length() > 0) {
            sb.append(" ) ");
        }

        return sb.toString();
    }

    private static void beforeAppend(StringBuilder sb, String c) {
        if (sb.length() == 0) {
            sb.append(" where ( ");
        } else {
            sb.append(c);
        }
    }
}
